/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.ContentConnection;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import listener.IDownloadListener;

/**
 *
 * @author kien.tran
 */
public class ImageItem extends ADetailItem implements IDownloadListener {

    private Image image;
    private String link;
    private int imageWidth = -1, imageHeight = -1;

    public ImageItem(String id, String title, String link, int imageWidth, int imageHeight) {
        super(id, title, "");
        this.link = link;
        this.imageWidth = imageWidth;
        this.imageHeight = imageHeight;
        new ImageDownloader(link, this).start();
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public int getImageWidth() {
        return imageWidth;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Image getImage() {
        return image;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }

        initItem();
        getHeight();

        int imgX;
        if (image != null) {
            imgX = (int) ((width - imageWidth) / 2);
            g.drawImage(image, imgX, y + style.getPadding().getTop(), Graphics.TOP | Graphics.LEFT);

        } else if (title.length() > 0) {
            int w = titleStyle.getWidth();
            if (w > imageWidth) {
                w = imageWidth + titleStyle.getPadding().getLeftAndRight();
            }
            imgX = (int) ((width - w) / 2);
            g.setColor(titleStyle.getSelectedBgColor());
            g.fillRect(imgX, y + style.getPadding().getTop(), w, height);
            for (int i = 0; i < titleRows.length; i++) {
                w = font.getFontNormal().stringWidth(titleRows[i]);
                imgX = (int) ((width - w) / 2);
                font.getFontNormal().drawString(g, titleRows[i], imgX, y + style.getPadding().getTop() + titleStyle.getPadding().getTop() + i * (font.getFontNormal().getHeight() + titleStyle.getRowHeight()), Graphics.TOP | Graphics.LEFT);
            }
        }
    }

    public int getHeight() {
        int h = titleStyle.getHeight();
        if (h < imageHeight) {
            h = imageHeight;
        }
        this.height = h + style.getPadding().getTopAndBottom();

        return this.height;
    }

    public void completedAction(Object result) {
        if (result != null && result instanceof Image) {
            this.image = (Image) result;
            this.imageHeight = image.getHeight();
            parent.repaint();
        } else {
            this.image = null;
        }
    }

    public class ImageDownloader implements Runnable {

        private String url;
        private IDownloadListener listener;

        public ImageDownloader(String url, IDownloadListener listener) {
            this.url = url;
            this.listener = listener;
        }

        public void run() {
            try {
                getImage(url);
            } catch (Exception e) {
            }
        }

        public void start() {
            Thread thread = new Thread(this);
            try {
                thread.start();
            } catch (Exception e) {
            }
        }

        private void getImage(String url) throws IOException {

            ContentConnection connection = (ContentConnection) Connector.open(url);
            DataInputStream iStrm = connection.openDataInputStream();
            ByteArrayOutputStream bStrm = null;
            Image im = null;

            try {
                // ContentConnection includes a length method
                byte imageData[];
                int length = (int) connection.getLength();
                if (length != -1) {
                    imageData = new byte[length];

                    // Read the png into an array
                    iStrm.readFully(imageData);
                } else { // Length not available...
                    bStrm = new ByteArrayOutputStream();

                    int ch;
                    while ((ch = iStrm.read()) != -1) {
                        bStrm.write(ch);
                    }

                    imageData = bStrm.toByteArray();
                }

                // Create the image from the byte array
                im = Image.createImage(imageData, 0, imageData.length);
            } finally {
                // Clean up
                if (connection != null) {
                    connection.close();
                }
                if (iStrm != null) {
                    iStrm.close();
                }
                if (bStrm != null) {
                    bStrm.close();
                }
            }

            // Return to the caller the status of the download
            if (im == null) {
                listener.completedAction(null);
            } else {
                listener.completedAction(im);
            }
        }
    }
}
